package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.model.common.business.dto.BusinessLoginDto;
import com.zhentao.model.common.business.dto.BusinessRegisterDto;
import com.zhentao.model.common.business.dto.BusinessUserDto;
import com.zhentao.model.common.business.pojo.BusinessShop;
import com.zhentao.model.common.business.pojo.BusinessUser;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.BusinessUserService;
import com.zhentao.mapper.BusinessUserMapper;
import com.zhentao.utils.common.AppJwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 贾晓婷
* @description 针对表【business_user】的数据库操作Service实现
* @createDate 2024-09-14 09:00:10
*/
@Service
public class BusinessUserServiceImpl extends ServiceImpl<BusinessUserMapper, BusinessUser>
    implements BusinessUserService{

    @Resource
    BusinessUserMapper businessUserMapper;
    @Override
    public ResponseResult login(BusinessLoginDto dto) {
        System.out.println("dto = " + dto);
        //        先进行判断
        if(StringUtils.isBlank(dto.getPhone())||StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"用户名或密码不能为空");
        }else {
//            获取用户信息
            QueryWrapper<BusinessUser> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(BusinessUser::getPhone,dto.getPhone());
            BusinessUser businessUser = this.businessUserMapper.selectOne(queryWrapper);
            System.out.println("手机号查询到的详细信息 = " + businessUser);
            if(businessUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"此用户不存在");
            }
//            将盐提取出来
            String pwd = businessUser.getSalt();
//            输入的密码提取出哎
            String pwd1 = dto.getPassword();
//            将输入的密码加盐
            pwd1= DigestUtils.md5DigestAsHex((pwd1+pwd).getBytes());
//            判断加密后的密码是否正确
            if(!pwd1.equals(businessUser.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"密码错误");
            }
            System.out.println("===============密码正确=================");
            Map<String,Object> map=new HashMap<>();
            map.put("token", AppJwtUtil.getToken(businessUser.getUserId().longValue()));
            businessUser.setSalt(null);
            businessUser.setPassword(null);
            map.put("user",businessUser);
            return ResponseResult.okResult(map);
        }
    }

    @Override
    public ResponseResult register(BusinessRegisterDto dto) {
        if(StringUtils.isBlank(dto.getPhone())||StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"用户名或密码不能为空");
        }else {
            QueryWrapper<BusinessUser> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(BusinessUser::getPhone,dto.getPhone());
            BusinessUser businessUser = this.businessUserMapper.selectOne(queryWrapper);
            System.out.println("手机号查询到的详细信息 = " + businessUser);
            if(businessUser != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"此用户已存在");
            }else {
                BusinessUser businessUser1 = new BusinessUser();
                BeanUtils.copyProperties(dto,businessUser1);

//                给密码加盐
                String Salt ="123";
                String pwd = businessUser1.getPassword();
                pwd= DigestUtils.md5DigestAsHex((Salt+pwd).getBytes());
                businessUser1.setPassword(pwd);
                businessUser1.setSalt(Salt);
                businessUser1.setCreateTime(new Date());
//                新增用户
                businessUserMapper.insert(businessUser1);
                return ResponseResult.okResult(businessUser1);
            }
        }
    }


    /**
     * 商家账号信息列表
     * @param businessUserDto
     * @return
     */
    @Override
    public ResponseResult selBusinessUser(BusinessUserDto businessUserDto) {
        //分页
        PageHelper.startPage(businessUserDto.getPageNum(),businessUserDto.getPageSize());
        //查询所有
        List<BusinessUser> businessUsers = businessUserMapper.selectList(Wrappers.<BusinessUser>lambdaQuery()
                //根据昵称查询
                .like(StringUtils.isNotBlank(businessUserDto.getNickname()), BusinessUser::getNickname, businessUserDto.getNickname())
                //根据手机号进行模糊查
                .like(StringUtils.isNotBlank(businessUserDto.getPhone()), BusinessUser::getPhone, businessUserDto.getPhone())
                //根据状态查询
                .eq(businessUserDto.getStatus() != null, BusinessUser::getStatus, businessUserDto.getStatus())
                //根据创建时间进行区间查
                .gt(businessUserDto.getStartTime() != null, BusinessUser::getCreateTime, businessUserDto.getStartTime())
                .lt(businessUserDto.getEndTime() != null, BusinessUser::getCreateTime, businessUserDto.getEndTime())
        );

        PageInfo<BusinessUser> businessUserPageInfo = new PageInfo<>(businessUsers);


        return ResponseResult.okResult(businessUserPageInfo);
    }
}




